//
//  DoctorIfo.m
//  EcmDoctor
//
//  Created by zhm on 16/2/16.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "DoctorIfo.h"
#import "NetWorkUtil.h"
#import <SSKeychain/SSKeychain.h>
#import "AppUtils.h"
#import "JPUSHService.h"

@implementation DoctorIfo

+ (void)loginWithUsername:(NSString *)username Password:(NSString *)password success:(void (^)(DoctorIfo *))success failure:(void (^)(NSError *))failure {
    
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数

    req.moduleName = @"DoctorRS";
    req.methodName = @"doctorLogin";
    [req setValue:username forKey:@"username"];
    [req setValue:password forKey:@"password"];    
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
     
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
          
            DoctorIfo *doctor = [[DoctorIfo alloc]initWithDictionary:userAttrs];
            NSLog(@"_________%@",responseObject);
            
            
            [JPUSHService setAlias:doctor.doctor_info_id callbackSelector:nil object:nil];
            
            [SSKeychain setPassword:doctor.phone forService:APP_SERVICE_NAME account:APP_PHONENUM_KEY];
            [SSKeychain setPassword:doctor.password forService:APP_SERVICE_NAME account:APP_PASSWORD_KEY];
            [SSKeychain setPassword:doctor.doctor_info_id forService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
            [SSKeychain setPassword:doctor.hx_id forService:APP_SERVICE_NAME account:APP_DOCTORHXID_KEY];
            
            [SSKeychain setPassword:doctor.hx_password forService:APP_SERVICE_NAME account:APP_DOCTORHXPASSWORD_KEY];
           
            [SSKeychain setPassword:doctor.token forService:[AppUtils appServiceName] account:[AppUtils tokenKey]];
            
            
            //存储医生基本信息和登陆状态，备用
            NSUserDefaults *doctorUser=[NSUserDefaults standardUserDefaults];
            [doctorUser setObject:doctor.name forKey:APP_DOCTORNAME_KEY];
            [doctorUser setObject:doctor.job_position forKey:APP_JOBPOSITION_KEY];
            [doctorUser setObject:doctor.special_des forKey:APP_SPECIALDES_KEY];
            [doctorUser setObject:@"dengluOk" forKey:APP_KEY];
            [doctorUser synchronize];
            success(doctor);
            
        } else{
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);

    } cache:NO];
    
}
+(void)getTheDoctorDetalInfoWith:(NSString *)doctorInfoId success:(void (^)(DoctorIfo *))success failure:(void (^)(NSError *))failure
{
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数
    req.moduleName = @"DoctorRS";
    req.methodName = @"getDoctorSimpleInfoById";
        [req setValue:doctorInfoId forKey:@"doctor_info_id"];
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
      
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
            
            DoctorIfo *doctor = [[DoctorIfo alloc]initWithDictionary:userAttrs];
            
            //存储医生详细信息，备用
            NSUserDefaults *doctorUser=[NSUserDefaults standardUserDefaults];
          
            [doctorUser setObject:doctor.office_hospital forKey:APP_OFFICEHOSPITAL_KEY];
            [doctorUser setObject:doctor.grade forKey:APP_GRADE_KEY];
            [doctorUser setObject:doctor.allordernum forKey:APP_ALLORDENUM_KEY];
            [doctorUser synchronize];
            success(doctor);
            
        } else{
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
    

}
+ (void)getDoctorWorkStateWithDoctorinfId:(NSString *)doctorinfId success:(void (^)(DoctorIfo *))success failure:(void (^)(NSError *))failure {
    
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数
    
    req.moduleName = @"DoctorRS";
    req.methodName = @"getDoctorWorkState";
    [req setValue:doctorinfId forKey:@"doctor_info_id"];

    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
           
            DoctorIfo *doctor = [[DoctorIfo alloc]initWithDictionary:userAttrs];
            
           //存储医生工作状态信息，备用
            NSUserDefaults *doctorUser=[NSUserDefaults standardUserDefaults];
            [doctorUser setObject:doctor.free_state forKey:APP_FREESTATE_KEY];
            [doctorUser setObject:doctor.online_state forKey:APP_ONLINESTATE_KEY];
            [doctorUser setObject:doctor.phone_state forKey:APP_PHONESTATE_KEY];
            [doctorUser setObject:doctor.addnum_state forKey:APP_ADDNUMSTATE_KEY];
             // [SSKeychain setPassword:doctor.token forService:[AppUtils appServiceName] account:[AppUtils tokenKey]];
            [doctorUser synchronize];
            success(doctor);
            
        } else{
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
    
}




@end
